/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.shuffling;

import java.util.Random;

/**
 *
 * @author Czarek
 */
public class RandomIdGenerator implements IShufflingIdGenerator{
    private final Random rand = new Random();
    
    private int getNext(final int prev, final boolean condition){
        //return ((prev%2!=0)?-1:1)*rand.nextInt(16*1024)*2+((condition^prev%2!=0)?1:0);
        return rand.nextInt();
    }

    @Override
    public void assureIds(IShufflable shuffable1, IShufflable shuffable2, boolean condition) {        
        Integer sid1 = shuffable1.getShuffleId();
        Integer sid2 = shuffable2.getShuffleId();
        if (sid1 != null && sid2 != null) {
            return;
        }
        final Integer prev = (sid1 != null ? sid1 : sid2);
        int iPrev =(prev != null) ? prev : ((true)?0:1);
        if (sid1 == null&&shuffable1!=shuffable2) {            
            int gen= getNext(iPrev, condition);
            shuffable1.setShuffleId(gen);
            iPrev=gen;
        }
        if (sid2 == null) {
            shuffable2.setShuffleId(getNext(iPrev, condition));
        }
        
    }

    @Override
    public String toString() {
        return "RandomIdGenerator{" + '}';
    }    
    
}
